//
//  Menu.swift
//  滚动菜单
//
//  Created by admin on 16/1/28.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class Menu: UIView {
    
    @IBOutlet weak var rootMenu: UIButton!
    private var menus:[UIButton] = []
    private var menusX:[CGFloat] = []
    @IBAction func menuClick(sender: UIButton) {
        if self.menus.first == nil
        {
            return
        }
        let duration:CFTimeInterval = 3
        let changeInfo = self.getChangeInfo()
        for var i = 0;i < self.menus.count;i++
        {
            let animationGroup = CAAnimationGroup()
            animationGroup.duration = duration
            let positionX = CAKeyframeAnimation(keyPath: "position.x")
            positionX.values = changeInfo[0][i] as? [AnyObject]
            let rotation = CAKeyframeAnimation(keyPath: "transform.rotation")
            rotation.values = changeInfo[1][i] as? [AnyObject]
            animationGroup.animations = [positionX,rotation]
            animationGroup.removedOnCompletion = false
            animationGroup.fillMode = kCAFillModeForwards
            self.menus[i].center.x = changeInfo[0][i].lastObject as! CGFloat
            self.menus[i].layer.addAnimation(animationGroup, forKey: nil)
        }
        let rootMenuRotation = CABasicAnimation(keyPath: "transform.rotation")
        rootMenuRotation.fromValue = (changeInfo[2] as! [CGFloat])[0]
        rootMenuRotation.toValue = (changeInfo[2] as! [CGFloat])[1]
        rootMenuRotation.removedOnCompletion = false
        rootMenuRotation.fillMode = kCAFillModeForwards
        rootMenuRotation.duration = duration
        self.rootMenu.layer.addAnimation(rootMenuRotation, forKey: nil)
        
    }
    func getChangeInfo()->NSMutableArray
    {
        let transforms:NSMutableArray = []
        let rotations:NSMutableArray = []
        var rootMenuRotation:[CGFloat] = []
        for var i = 0;i < self.menus.count;i++
        {
            if self.menus.first?.layer.position.x == self.rootMenu.center.x
            {
                transforms.addObject([self.rootMenu.center.x,self.menusX[i] * 1.2,self.menusX[i]])
                rotations.addObject([0,M_PI * 4,M_PI * 2])
                rootMenuRotation = [0,CGFloat(M_PI * 0.25)]
            }
            else
            {
                transforms.addObject([self.menus[i].center.x,self.menus[i].center.x * 1.2,self.rootMenu.center.x])
                rotations.addObject([0,M_PI * 2,-M_PI * 2])
                rootMenuRotation = [CGFloat(M_PI * 0.25),0]
            }
        }
        return NSMutableArray(array: [transforms,rotations,rootMenuRotation])
    }
    // 添加子菜单方法,参数为图片名称
    func addMenu(name:String)
    {
        // 获得新添加的图片的名称
        let image = UIImage(imageLiteral: name)
        // 初始化当前子菜单按钮
        let button = UIButton(type: UIButtonType.Custom)
        // 设置图片
        button.setImage(image, forState: UIControlState.Normal)
        // 设置宽高
        button.bounds = CGRectMake(0, 0, 40, 40)
        // 添加进类属性
        self.menus.append(button)
        // 添加到父view
        self.addSubview(button)
        // 需要重新计算各个子菜单的位置，所以需要先置空之前的信息
        self.menusX = []
        // 获得magin
        let margin = ((self.frame.size.width * 0.8 - CGRectGetMaxX(self.rootMenu.frame)) - (CGFloat(self.menus.count) * button.bounds.size.width)) / CGFloat(self.menus.count)
        // 计算各个子控件的位置
        for var i = 0;i < self.menus.count;i++
        {
            // 获得上个控件的中心点x轴坐标
            var preButtonX = self.rootMenu.center.x
            // 如果不是第一个，就获得上个控件的中心点x轴坐标
            if i != 0
            {
                preButtonX = self.menusX[i - 1]
            }
            // 初始化当前位置
            self.menus[i].center = self.rootMenu.center
            // 记录下button位置信息
            self.menusX.append(preButtonX + margin + button.bounds.size.width)
        }
        self.bringSubviewToFront(self.rootMenu)
    }
}
